#include "widget.h"
#include "ui_widget.h"

#include "login.h"
#include "regist.h"

Widget::Widget(QMainWindow *parent) :
    QMainWindow(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    setFixedSize(800, 480);

    this->setObjectName("widget");
    this->setStyleSheet("#widget{background-image: url(:/res/beijing.jpg);}");

    key = new KeyBoard(this);
    key->setGeometry(10, 320, 780, 150);
    key->listview->setFixedHeight(150);
    key->hide();
    key->init_small();
    connect(key, &KeyBoard::itemClick, this, &Widget::keyPress);

    ui->name_lineEdit->installEventFilter(this);
    ui->name_lineEdit->setFocusPolicy(Qt::ClickFocus);

    ui->passwd_lineEdit->installEventFilter(this);
    ui->passwd_lineEdit->setFocusPolicy(Qt::ClickFocus);
    ui->passwd_lineEdit->setEchoMode(QLineEdit::Password);

    admin = new Admin();
    admin->hide();
#ifdef SUPPORT_ARM
    connect(Thread::Get(), &Thread::cppSigCard, this, [&](QString id)
    {
        welog_debug() << "id : " << id;
        if(isFocus)
        {
            if(id == "495292049")
            {
                if(admin->isHidden())
                    admin->show();
            }
            else if(!isShowUser)
            {
                User_t user = Sqlite::Get()->Select_user(id);
                if(!user.name.isEmpty())
                {
                    isShowUser = true;
                    User *u = new User(user);
                    connect(u, &User::sigClose, this, [&]{
                        isShowUser = false;
                    });
                    u->show();
                }
            }
        }
    });
#endif
}

Widget::~Widget()
{
    delete ui;
}

void Widget::keyPress(QString ch)
{
    qDebug() << ch;
    if(ch == "A/a")
    {
        key->init_larget();
    }
    else if(ch == "a/A")
    {
        key->init_small();
    }
    else if(ch == "123L" || ch == "123l" || ch == "123")
    {
        key->init_number();
    }/*
    else if(ch == "&")
    {
        if(nameFocuse)
            ui->name_lineEdit->insert("&");
        else
            ui->passwd_lineEdit->insert("&");
    }*/
    else if(ch == "->")
    {
        deleteBtn_click();
    }
    else
    {
        QLineEdit *lineEdit = nullptr;
        switch (choose)
        {
            case NAME:
            {
                lineEdit = ui->name_lineEdit;
                break;
            }
            case PASSWD:
            {
                lineEdit = ui->passwd_lineEdit;
                break;
            }
            default:
                break;
        }
        if(lineEdit != nullptr)
        {
            lineEdit->insert(ch);
        }
    }
}

bool Widget::eventFilter(QObject * watched, QEvent * event)
{    
    if (ui->name_lineEdit == watched)
    {
        if (event->type() == QEvent::MouseButtonPress)
        {
            qDebug() << "MouseButtonPress name_lineEdit";
            choose = NAME;
            key->show();
        }
    }
    else if(ui->passwd_lineEdit == watched)
    {
        if (event->type() == QEvent::MouseButtonPress)
        {
            qDebug() << "MouseButtonPress passwd_lineEdit";
            choose = PASSWD;
            key->init_number();
            key->show();
        }
    }
    else
    {
        welog_debug() << "event : " << event->type();
    }

    return QWidget::eventFilter(watched, event);
}

void Widget::mouseReleaseEvent(QMouseEvent *)
{
    qDebug() << "mouseReleaseEvent";
    key->hide();
}

void Widget::deleteBtn_click()
{
    QLineEdit *lineEdit = nullptr;
    QString text = "";
    int pos = -1;
    switch (choose)
    {
        case NAME:
        {
            lineEdit = ui->name_lineEdit;
            break;
        }
        case PASSWD:
        {
            lineEdit = ui->passwd_lineEdit;
            break;
        }
        default:
            break;
    }
    if(lineEdit != nullptr)
    {
        text = lineEdit->text();
        if(text.isEmpty())
        {
            lineEdit->setFocus();
            return ;
        }
        pos = lineEdit->cursorPosition();

        if(pos <= text.size() && pos != 0)
        {
            text.remove(pos-1, 1);
            lineEdit->setText(text);
            lineEdit->setCursorPosition(pos-1);
        }
    }
}

void Widget::on_login_pushButton_clicked()
{
    QString id = ui->name_lineEdit->text();
    QString passwd = ui->passwd_lineEdit->text();
    if(!id.isEmpty() && !passwd.isEmpty())
    {
        if(user == 1)
        {
            User_t user = Sqlite::Get()->Select_user(id, passwd);
            if(user.name.isEmpty())
            {
                QMessageBox::about(this, "error", "id或密码错误！");
            }
            else
            {
                ui->name_lineEdit->clear();
                ui->passwd_lineEdit->clear();
                User *u = new User(user);
                u->show();
            }
        }
        else
        {
            if(id == "admin" && passwd == "admin")
            {
                if(admin->isHidden())
                {
                    admin->show();
                }
            }
            else
                QMessageBox::about(this, "error", "id或密码错误！");
        }
    }
    else
    {
        QMessageBox::about(this, "error", "输入不能为空！！");
    }
}

void Widget::on_regist_pushButton_clicked()
{
    Regist *r = new Regist(this);
    r->show();
}

void Widget::on_admin_pushButton_clicked()
{
    Login *l = new Login(0);
    l->show();
}
